package com.emm.yixun.website.listeners;

import com.emm.yixun.common.enums.ResponseEnums;
import com.emm.yixun.common.model.*;
import com.emm.yixun.common.model.dto.*;
import com.emm.yixun.common.model.enums.*;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.response.ResponseList;
import com.emm.yixun.common.utils.BeanUtil;
import com.emm.yixun.common.utils.DateStyle;
import com.emm.yixun.common.utils.DateUtils;
import com.emm.yixun.customer.service.CustomerServiceFacade;
import com.emm.yixun.customer.service.PayDepositServiceFacade;
import com.emm.yixun.customer.service.SignedServiceFacade;
import com.emm.yixun.customer.service.SubscribeServiceFacade;
import com.emm.yixun.customer.service.channel.ChannelSendHttpMessageService;
import com.emm.yixun.estate.service.ProjectRoomServiceFacade;
import com.emm.yixun.marketing.service.BusinessAbnormaServiceFacade;
import com.emm.yixun.marketing.service.HaveReceiveRefundServiceFacade;
import com.emm.yixun.marketing.service.NeedReceiveRefundServiceFacade;
import com.emm.yixun.marketing.service.RoomChangeHistoryServiceFacade;
import com.emm.yixun.system.service.notice.signed.SignedCheckOutNoticeFacade;
import com.emm.yixun.website.common.utils.SpringWebContextUtil;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.service.ActivitiProcessServiceImpl;
import com.emm.yixun.website.service.pmo.LevelProjectDataService;
import com.emm.yixun.website.service.pmo.PmoPushService;
import com.emm.yixun.website.service.system.log.ActCommitLogService;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

public class CheckOutListeners implements Serializable, ExecutionListener{
	private static final long serialVersionUID = 1L;
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Override
	public void notify(DelegateExecution execution) throws Exception {
		// TODO Auto-generated method stub
		BusinessAbnormaServiceFacade businessAbnormaServiceFacade=getBusinessAbnormaServiceFacade();
		SignedServiceFacade signedServiceFacade=getSignedServiceFacade();
		PayDepositServiceFacade payDepositServiceFacade=getPayDepositServiceFacade();
		
		String businessAbnormaId=execution.getProcessBusinessKey();
		String processInstanceId=execution.getProcessInstanceId();
		Response<BusinessAbnorma> businessAbnormaResponse=businessAbnormaServiceFacade.findById(Long.valueOf(businessAbnormaId));
		if(!businessAbnormaResponse.isSuccess()){
			throw new RuntimeException("退房记录不存在");
		}
		BusinessAbnorma businessAbnorma=businessAbnormaResponse.getEntity();
		if(null==businessAbnorma){
			throw new RuntimeException("退房记录不存在");
		}
		long signedId=businessAbnorma.getBusinessId();
		Response<Signed> signedRespponse=signedServiceFacade.findById(signedId);
		if(!signedRespponse.isSuccess()){
			throw new RuntimeException("退房流程：签约记录不存在");
		}
		Signed signed = signedRespponse.getEntity();
		if(null==signed){
			throw new RuntimeException("退房流程：签约记录不存在");
		}
		//判断是否审核通过
		if(getActivitiProcessServiceImpl().isCheckPass(processInstanceId)){
			
			boolean flag = checkAuditCommit(businessAbnorma);
			if(!flag) {
				throw new Exception(ResponseEnums.RESPONSEENUMS_999999.getName());
			}
			
			/**
			 * 审核通过
			 * 1.更新异常业务表的退房审核状态为审批通过
			 * 2.更新签约记录
			 *  2.1、更新原签约记录第二业务标志为已退房
			 *	2.2、新增签约记录,业务标识为已退房
			 * 3.根据房源是否有认筹、认购更新房源状态为未售、认筹、认购
			 * 4.记录房源变更记录签约转未售
			 * 5.生成应退记录
			 * 	5.1、生成签约退房记录的待收款退款(待收款退款,根据老签约记录的实际已收生成)
			 * 	5.2、生成签约退房记录的已收款退款
			 * 6.更新客户状态
			 * 7.推送通知
			 */
			
			// 1.更新退房审核状态为审批通过
			BusinessAbnormaDto updateBADto=new BusinessAbnormaDto();
			updateBADto.setId(businessAbnorma.getId());
			updateBADto.setAuditStatus(AuditStatusEnum.SUCCESS.getNo());
			updateBADto.setCheckDate(new Date());
			Response<Integer> upResponse=businessAbnormaServiceFacade.updateByPrimaryKey(updateBADto, true);
			if(!upResponse.isSuccess() || upResponse.getEntity()==null || upResponse.getEntity()<=0){
				throw new RuntimeException("退房流程：更新退房审批状态失败");
			}
			//2.更新签约记录的业务标注为已退房 
			//2.1、更新原签约记录第二业务标志为已退房
			SignedDto signedDto=new SignedDto();
			signedDto.setId(businessAbnorma.getBusinessId());
			signedDto.setBusinessSecStatus(BusinessStatusEnum.CHECKWARDS.getNo());
			upResponse=signedServiceFacade.updateByPrimaryKey(signedDto, true);
			if(!upResponse.isSuccess() || upResponse.getEntity()==null || upResponse.getEntity()<=0){
				throw new RuntimeException("退房流程：更新签约记录业务标志失败");
			}
			//2.2、根据原签约记录,生成新的签约退款
			Signed newSigned = new Signed();
			signedDto = new SignedDto();
			BeanUtils.copyProperties(signed, signedDto);
			signedDto.setBusinessStatus(BusinessStatusEnum.CHECKWARDS.getNo());
			signedDto.setBusinessSecStatus(BusinessStatusEnum.CHECKWARDS.getNo());
			signedDto.setCheckDate(new Date());
			signedDto.setOldSignedId(signed.getId());
			Response<SignedDto> newSignedDtoRes = signedServiceFacade.save(signedDto);
			newSigned = newSignedDtoRes.getEntity();

			//3.根据房源是否有认筹、认购更新房源状态为未售、认筹、认购
			RoomChangeHistoryDto dto=new RoomChangeHistoryDto();
			dto = changeRoomStatus(dto, signed);
			
			//4.记录房源变更记录签约转未售
			recordRoomChange(dto, signed);
    		
			//5.生成应退记录
			refundOperation(signed, newSigned);
			
			/**
    		 * 2017-08-11
    		 * 判断是否已存在待收款记录
    		 * 如有待收款记录，则删除
    		 */
    		Boolean needFlag = isDelByDto(signedId);
			if(!needFlag){
    			throw new Exception("删除待收款记录失败");
    		}
			
			/**
			 * 6.更改客户交易状态为认购，判断是否多套交易并更新状态 todo
			 * @param bugType 业务类型： 1 认筹、2 认购、3 签约、4退房
			 * @param busId 对应业务类型Id
			 * @param customerId 客户Id
			 * @param userId 用户Id
			 */
			payDepositServiceFacade.getCustomerEntity("4", Long.valueOf(execution.getProcessBusinessKey()),
					signed.getCustomerId() , signed.getUpdateUserId());
			
			/**
			 * 2019-02增加
			 * 如果是认购转签约，执行退认购逻辑
			 */
			subscribeRetreat(signed.getRoomId(), signed.getCustomerId(), signed.getOldSignedId());
    		
			/**
			 * 查询客户业务状态，并同步更新客户状态
			 *//*
			CustomerServiceFacade customerServiceFacade = getCustomerServiceFacade();
			String busStatus = customerServiceFacade.queryCustomerBusiness(signed.getCustomerId());
			CustomerDto cusDto = new CustomerDto();
			cusDto.setId(signed.getCustomerId());
			if(busStatus != null){
				cusDto.setCustomerStatus(CustomerStatusEnum.getByDescription(busStatus).getNo());
				Response<Integer> responseCusDto = customerServiceFacade.updateByPrimaryKey(cusDto, true);
				if(!responseCusDto.isSuccess()){
					throw new Exception("客户状态更新出错");
				}
			}else{
				Response<Customer> customerEntity = customerServiceFacade.findById(signed.getCustomerId());
				if(customerEntity.getEntity().getCustomerStatusBk() == null){
					cusDto.setCustomerStatus(CustomerStatusEnum.VISIT.getNo());
				}else{
					cusDto.setCustomerStatus(customerEntity.getEntity().getCustomerStatusBk());
				}
				Response<Integer> responseCusDto = customerServiceFacade.updateByPrimaryKey(cusDto, true);
				if(!responseCusDto.isSuccess()){
					throw new Exception("客户状态更新出错");
				}
			}*/

			CustomerServiceFacade customerServiceFacade = getCustomerServiceFacade();
			customerServiceFacade.changeCustomerLevel(signed.getCustomerId(), IntentionLevelEnum.C.getNo());

			/**
			 * 推送到渠道
			 */
			getChannelSendHttpMessageService().sendTransaction(signed.getId(), TransTypeEnum.SIGNED.getNo(), true);
    		//7.推送通知
			try {
				/**
				 * （退房流程审核通过）推送消息到app
				 * @author ny
				 * @param param noticeType 提醒类型, noticeBusinessId 业务主键Id
				 * @return 退房审核结果【${auditStatus}】房号：${roomName}；客户：${customer};
				 */
				NoticeEntity noticeEntity = new NoticeEntity();
				noticeEntity.setNoticeBusinessId(signed.getId());
				noticeEntity.setNoticeType("11");
				noticeEntity.setProcessInstanceId(processInstanceId);
				noticeEntity.setAudStatus("3");
				getSignedCheckOutNoticeFacade().createSignedCheckOutNotice(noticeEntity);

				//添加推送PMO待推送记录
				String pmoProjectCode = getLevelProjectDataService().getProjectPmoCode(signed.getProjectId());
				String pushMonth = DateUtils.formatDate(new Date(), DateStyle.YYYYMM.getValue());
				PmoPushService pmoPushService = getPmoPushService();
				pmoPushService.addPmoPushData(signed.getProjectId(), pmoProjectCode, pushMonth, processInstanceId, "退房");

			} catch (Exception e) {
				logger.error("退房审批推送数据失败", e);
			}
		}else{
			
			boolean flag = checkAuditCommit(businessAbnorma);
			if(!flag) {
				throw new Exception(ResponseEnums.RESPONSEENUMS_999999.getName());
			}
			
			/**
			 * 审核不通过
			 * 1.更新退房审核状态为审批不通过
			 */
			BusinessAbnormaDto updateBADto=new BusinessAbnormaDto();
			updateBADto.setId(businessAbnorma.getId());
			updateBADto.setAuditStatus(AuditStatusEnum.REJECT.getNo());
			updateBADto.setCheckDate(new Date());
			businessAbnormaServiceFacade.updateByPrimaryKey(updateBADto, true);
			
			/**
			 * （退房流程审核不通过）推送消息到app
			 * @author ny
			 * @param param noticeType 提醒类型, noticeBusinessId 业务主键Id
			 * @return 退房审核结果【${auditStatus}】房号：${roomName}；客户：${customer};
			 */
			NoticeEntity noticeEntity = new NoticeEntity();
			noticeEntity.setNoticeBusinessId(signed.getId());
			noticeEntity.setNoticeType("11");
			noticeEntity.setProcessInstanceId(processInstanceId);
			noticeEntity.setAudStatus("2");
			getSignedCheckOutNoticeFacade().createSignedCheckOutNotice(noticeEntity);
		}
	}

	/**
	 * 变更房间状态
	 * @param dto
	 * @param signed
	 * @return
	 */
	private RoomChangeHistoryDto changeRoomStatus(RoomChangeHistoryDto dto, Signed signed) throws Exception{
		dto.setRemark("房源由【签约】转为【可售】");
		//认购转签约：判断认购ID是否存在，存在情况下，判断该房间是否还有认购记录，有则房间状态回到认购，否则回到可售状态，不存在情况下，往下判断
		if(signed.getSubscribeId()!=null){
			SubscribeDto subscribeDto = new SubscribeDto();
			subscribeDto.setM_auditStatus(" and t.audit_status in (1,3) and t.business_status in (1,4) and t.business_sec_status not in (2,3)");
			subscribeDto.setRoomId(signed.getRoomId());
			ResponseList<Subscribe> responseList = getSubscribeServiceFacade().findByDtoJ(subscribeDto);
			if(responseList.getEntity()==null||responseList.getEntity().size()==0){
				ProjectRoomDto projectRoomDto = new ProjectRoomDto();
				projectRoomDto.setId(signed.getRoomId());
				projectRoomDto.setRoomStatus(RoomStatusEnum.VENDIBILITY.getNo());//可售
				Response<Integer> responseRoomDto=getProjectRoomServiceFacade().updateByPrimaryKey(projectRoomDto, true);
				if(!responseRoomDto.isSuccess()){
					throw new Exception("房间状态为可售出错");
				}
			}else{
				ProjectRoomDto projectRoomDto = new ProjectRoomDto();
				projectRoomDto.setId(signed.getRoomId());
				projectRoomDto.setRoomStatus(RoomStatusEnum.RESERVE.getNo());//认购
				Response<Integer> responseRoomDto=getProjectRoomServiceFacade().updateByPrimaryKey(projectRoomDto, true);
				if(!responseRoomDto.isSuccess()){
					throw new Exception("房间状态为认购出错");
				}
				dto.setRemark("房源由【签约】转为【"+RoomStatusEnum.RESERVE.getDes()+"】");
			}
		}else if(signed.getPayDepositId()!=null){
			//认筹转签约：判断认筹ID是否存在，存在情况下，判断该房间是否还有认筹记录，有则房间状态回到认筹，否则回到可售状态，不存在情况下，往下判断
			PayDepositDto payDepositDto = new PayDepositDto();
			payDepositDto.setM_auditStatus(" and t.audit_status in (1,3) and t.business_status in (1,4) ");
			payDepositDto.setRoomId(signed.getRoomId());
			ResponseList<PayDeposit> responseList = getPayDepositServiceFacade().findByDtoJ(payDepositDto);
			if(responseList.getEntity()==null||responseList.getEntity().size()==0){
				ProjectRoomDto projectRoomDto = new ProjectRoomDto();
				projectRoomDto.setId(signed.getRoomId());
				projectRoomDto.setRoomStatus(RoomStatusEnum.VENDIBILITY.getNo());//可售
				Response<Integer> responseRoomDto=getProjectRoomServiceFacade().updateByPrimaryKey(projectRoomDto, true);
				if(!responseRoomDto.isSuccess()){
					throw new Exception("房间状态为可售出错");
				}
			}else{
				ProjectRoomDto projectRoomDto = new ProjectRoomDto();
				projectRoomDto.setId(signed.getRoomId());
				projectRoomDto.setRoomStatus(RoomStatusEnum.RESERVE.getNo());//认筹
				Response<Integer> responseRoomDto=getProjectRoomServiceFacade().updateByPrimaryKey(projectRoomDto, true);
				if(!responseRoomDto.isSuccess()){
					throw new Exception("房间状态为认筹出错");
				}
				dto.setRemark("房源由【签约】转为【"+RoomStatusEnum.RESERVE.getDes()+"】");
			}
		}else{
			//直接签约：判断该房间是否还有认筹记录，有则房间状态回到认筹，否则回到可售状态
			PayDepositDto payDepositDto = new PayDepositDto();
			payDepositDto.setM_auditStatus(" and t.audit_status in (1,3) and t.business_status in (1,4) ");
			payDepositDto.setRoomId(signed.getRoomId());
			ResponseList<PayDeposit> responseList = getPayDepositServiceFacade().findByDtoJ(payDepositDto);
			if(responseList.getEntity()==null||responseList.getEntity().size()==0){
				ProjectRoomDto projectRoomDto = new ProjectRoomDto();
				projectRoomDto.setId(signed.getRoomId());
				projectRoomDto.setRoomStatus(RoomStatusEnum.VENDIBILITY.getNo());//可售
				Response<Integer> responseRoomDto=getProjectRoomServiceFacade().updateByPrimaryKey(projectRoomDto, true);
				if(!responseRoomDto.isSuccess()){
					throw new Exception("房间状态为可售出错");
				}
			}else{
				ProjectRoomDto projectRoomDto = new ProjectRoomDto();
				projectRoomDto.setId(signed.getRoomId());
				projectRoomDto.setRoomStatus(RoomStatusEnum.RESERVE.getNo());//认筹
				Response<Integer> responseRoomDto=getProjectRoomServiceFacade().updateByPrimaryKey(projectRoomDto, true);
				if(!responseRoomDto.isSuccess()){
					throw new Exception("房间状态为认筹出错");
				}
				dto.setRemark("房源由【签约】转为【"+RoomStatusEnum.RESERVE.getDes()+"】");
			}
		}
		return dto;
	}
	
	/**
	 * 记录房源变更情况
	 * @param dto
	 * @param signed
	 */
	public void recordRoomChange(RoomChangeHistoryDto dto, Signed signed) throws Exception{
		dto.setRoomId(signed.getRoomId());
		dto.setBusinessDynamics("退房");
		dto.setMerchantId(signed.getMerchantId());
		dto.setAccessCode(signed.getAccessCode());
		dto.setCreateUserId(signed.getCreateUserId());
		dto.setUserCode(signed.getUserCode());
		dto.setDelFlag("0");
		dto.setUpdateUserId(signed.getUpdateUserId());
		dto.setUpdateTime(new Date());
		Response<RoomChangeHistoryDto> rchSaveResponse = getRoomChangeHistoryServiceFacade().save(dto);
		if(!rchSaveResponse.isSuccess()){
			throw new RuntimeException(rchSaveResponse.getResultMessage());
		}
	}
	
	/**
	 * 处理退款操作
	 * @param signed
	 * @param newSigned
	 */
	private void refundOperation(Signed signed, Signed newSigned) throws Exception {
		HaveReceiveRefundServiceFacade  haveReceiveRefundServiceFacade = getHaveReceiveRefundServiceFacade();
		//原签约已收金额
		HaveReceiveRefundDto haveReceiveRefundDto=new HaveReceiveRefundDto();
		haveReceiveRefundDto.setRelationId(signed.getId());
		haveReceiveRefundDto.setReceivablesType(ReceivablesTypeEnum.SIGNED.getNo());//签约
		haveReceiveRefundDto.setDocType("0");//收款
		Double receiveMoney=haveReceiveRefundServiceFacade.haveReceiveAmount(haveReceiveRefundDto);
		//存在已收金额,处理退款信息
		if(null!=receiveMoney && receiveMoney>0){
			NeedReceiveRefundDto needReceiveRefundDto=new NeedReceiveRefundDto();
			needReceiveRefundDto.setRelationId(newSigned.getId());
			needReceiveRefundDto.setBusinessType(BusinessTypeEnum.SIGNED.getNo());
			needReceiveRefundDto.setPaymentMethod(PaymentMethodEnum.LUMP_SUM.getNo());
			needReceiveRefundDto.setReceivablesDate(Calendar.getInstance().getTime());
			needReceiveRefundDto.setReceivablesFunds("一次性付款");
			needReceiveRefundDto.setTotalReceivablesAmount(new BigDecimal(receiveMoney));//应退款
			needReceiveRefundDto.setCustomerId(newSigned.getCustomerId());
			needReceiveRefundDto.setRoomId(newSigned.getRoomId());
			needReceiveRefundDto.setStatus(NeedStatusEnum.YES.getNo());
			needReceiveRefundDto.setPostponedStatus(PostponedStatusEnum.NO.getNo());
			needReceiveRefundDto.setDocType(DocTypeEnum.REFUND.getNo());
			needReceiveRefundDto.setRemark("签约退房退款");
			needReceiveRefundDto.setHousingFundType(HousingFundTypeEnum.FUNDTYPE_21.getNo());
			needReceiveRefundDto.setBusinessFlag(BusinessFlagEnum.BUSINESSFLAG_14.getNo());
			needReceiveRefundDto.setMerchantId(newSigned.getMerchantId());
			needReceiveRefundDto.setUserCode(newSigned.getUserCode());
			needReceiveRefundDto.setAccessCode(newSigned.getAccessCode());
			needReceiveRefundDto.setCreateUserId(newSigned.getCreateUserId());
			needReceiveRefundDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			needReceiveRefundDto.setUpdateUserId(newSigned.getUpdateUserId());
			needReceiveRefundDto.setUpdateTime(new Date());
			Response<NeedReceiveRefundDto> nrrdSaveResponse = getNeedReceiveRefundServiceFacade().save(needReceiveRefundDto);
			if(!nrrdSaveResponse.isSuccess()){
    			throw new Exception(nrrdSaveResponse.getResultMessage());
    		}
			
			/**
			 * 2017-08-11
			 * 增加自动退款功能
			 * 退款审核成功后，待退款直接变成已退款，不需要用户操作
			 */
			NeedReceiveRefundDto needDto = nrrdSaveResponse.getEntity();
			Boolean haveFlag = refund(needDto);
			if(!haveFlag){
    			throw new Exception("自动退款功能执行失败");
    		}
		}
	}
	
	/**
	 * 2017-08-11
	 * 增加自动退款功能
	 * 退款审核成功后，待退款直接变成已退款，不需要用户操作
	 */
	private boolean refund(NeedReceiveRefundDto dto){
		User user = ShiroUtil.getCurrentUser();
		HaveReceiveRefundDto entity = new HaveReceiveRefundDto();
		BeanUtils.copyProperties(dto, entity);
		entity.setId(null);
		entity.setReceivablesType(dto.getBusinessType()); // 业务类型
		entity.setRelationId(dto.getRelationId());// 业务ID
		entity.setReceivablesId(dto.getId()); // 应收款ID
		entity.setDeductibleAmount(new BigDecimal("0")); // 抵扣金额
		entity.setTransferStatus(TransferStatusEnum.NORMAL.getNo()); // 转账状态 0 正常 1 已转账
		entity.setOpSource("0");
		entity.setPaymentMethod(PaymentMethodEnum.LUMP_SUM.getNo());
		entity.setReceivablesFunds("一次性付款");
		entity.setReceivablesDate(Calendar.getInstance().getTime());
		entity.setBankAccount("默认工商银行-6221234567890"); // 银行账号  
		entity.setStatementId(1L); // 结算人员
		if(null != user) {
			entity.setStatementId(user.getId()); // 结算人员
		}
		entity.setStatementType(Long.parseLong(StatementTypeEnum.CASH.getNo())); // 结算类型
		entity.setDocType("1"); // 已退款
		entity.setReChecked("0"); // 未复核
		entity.setStatementDate(new Date()); // 结算日期
		
		boolean flag = getHaveReceiveRefundServiceFacade()
				       .addRefund(entity, dto.getId(), new BigDecimal("0"), 
				    		   	  dto.getTotalReceivablesAmount(), "need");
		return flag;
	}
	
	/**
	 * 2017-08-11
	 * 判断是否已存在待收款记录
	 * 如有待收款记录，则删除
	 */
	private boolean isDelByDto(Long id){
		NeedReceiveRefundDto dto = new NeedReceiveRefundDto();
		dto.setRelationId(id); //认购ID
		dto.setBusinessType(BusinessTypeEnum.SIGNED.getNo()); //认购
		dto.setStatus(NeedStatusEnum.NO.getNo()); // 未结清
		dto.setDocType(DocTypeEnum.COLLECTION.getNo()); //待收款
		dto.setDelFlag(DelFlagEnum.UNDEL.getNo()); 
		boolean flag = getNeedReceiveRefundServiceFacade().isDelByDto(dto);
		
		return flag;
	}
	
	/**
	 * 前置判断,审核记录是否已经提交,防止重复提交
	 * 产生原因: 提交审核,后台处理中时,刷新页面再次提交审核,会重复
	 * true: 可继续执行
	 * false: 不可执行
	 */
	public boolean checkAuditCommit(BusinessAbnorma entity) {
		// 业务数据为null时,默认通过.为了不改变老业务,不影响流程正常行走
		if (null == entity) {
			return true;
		}
		return getActcommitLogService().checkActCommit(entity.getAuditStatus(), entity.getMerchantId(), entity.getId(),
				entity.getProcInstId(), "退房", "PC");
	}
	
	/**
	 * 2019-02增加
	 * 退认购逻辑
	 * junze.liu
	 */
	private String subscribeRetreat(Long roomId, Long customerId, Long oldSignedId) {
		//查询认购记录
		SubscribeDto subscribeDto = new SubscribeDto();
		subscribeDto.setM_auditStatus(" and t.audit_status = '3' and t.business_status = '2' and t.business_sec_status is null");
		subscribeDto.setRoomId(roomId);
		subscribeDto.setCustomerId(customerId);
		ResponseList<Subscribe> responseList = getSubscribeServiceFacade().findByDtoJ(subscribeDto);
		//不是认购转签约时，直接返回
		if(responseList.getEntity() == null || responseList.getEntity().size() == 0){
			return null;
		}
		//是认购转签约时,执行退认购逻辑
		Subscribe subscribe = (Subscribe)responseList.getEntity().get(0);
		//修改认购记录为退认购
		SubscribeDto updatePddDto = new SubscribeDto();
		updatePddDto.setId(subscribe.getId());
		updatePddDto.setBusinessStatus(SubscribeBusinessStatusEnum.SUBSCRIBE.getNo());
		updatePddDto.setBusinessSecStatus("5");
		updatePddDto.setIsCkeckOut("1"); //标识签约退房
		Response<Integer> responseUudate = getSubscribeServiceFacade().updateByPrimaryKey(updatePddDto, true);
		
		//生成新的认购记录 认购状态为退认购
		SubscribeDto createNewSubscribe = new SubscribeDto();
		BeanUtil.convert2T(createNewSubscribe, subscribe);
		createNewSubscribe.setBusinessStatus("5");
		createNewSubscribe.setBusinessSecStatus("5");
		createNewSubscribe.setIsCkeckOut("1"); //标识签约退房
		SubscribeDto addSub = getSubscribeServiceFacade().save(createNewSubscribe).getEntity();
		
		return null;
	}
	
	private ActCommitLogService getActcommitLogService(){
		return (ActCommitLogService)SpringWebContextUtil.getApplicationContext().getBean("actCommitLogService");
	}
	
	private SignedCheckOutNoticeFacade getSignedCheckOutNoticeFacade(){
		return (SignedCheckOutNoticeFacade)SpringWebContextUtil.getApplicationContext().getBean("signedCheckOutNoticeFacade");
	}
	
	private BusinessAbnormaServiceFacade getBusinessAbnormaServiceFacade(){
		return (BusinessAbnormaServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("businessAbnormaServiceFacade");
	}
	
	private ActivitiProcessServiceImpl getActivitiProcessServiceImpl(){
		return (ActivitiProcessServiceImpl)SpringWebContextUtil.getApplicationContext().getBean("activitiProcessServiceImpl");
	}
	
	private PayDepositServiceFacade getPayDepositServiceFacade(){
		return (PayDepositServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("payDepositServiceFacade");
	}
	
	private SubscribeServiceFacade getSubscribeServiceFacade(){
		return (SubscribeServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("subscribeServiceFacade");
	}
	
	private SignedServiceFacade getSignedServiceFacade(){
		return (SignedServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("signedServiceFacade");
	}
	
	
	private ProjectRoomServiceFacade getProjectRoomServiceFacade(){
		return (ProjectRoomServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("projectRoomServiceFacade");
	}
	
	private RoomChangeHistoryServiceFacade getRoomChangeHistoryServiceFacade(){
		return (RoomChangeHistoryServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("roomChangeHistoryServiceFacade");
	}
	
	private NeedReceiveRefundServiceFacade getNeedReceiveRefundServiceFacade(){
		return (NeedReceiveRefundServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("needReceiveRefundServiceFacade");
	}
	
	private HaveReceiveRefundServiceFacade getHaveReceiveRefundServiceFacade(){
		return (HaveReceiveRefundServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("haveReceiveRefundServiceFacade");
	}

	private CustomerServiceFacade getCustomerServiceFacade(){
		return (CustomerServiceFacade)SpringWebContextUtil.getApplicationContext().getBean("customerServiceFacade");
	}

	private LevelProjectDataService getLevelProjectDataService(){
		return (LevelProjectDataService)SpringWebContextUtil.getApplicationContext().getBean("levelProjectDataService");
	}

	private PmoPushService getPmoPushService(){
		return (PmoPushService)SpringWebContextUtil.getApplicationContext().getBean("pmoPushService");
	}

	private ChannelSendHttpMessageService getChannelSendHttpMessageService(){
		return (ChannelSendHttpMessageService)SpringWebContextUtil.getApplicationContext().getBean("channelSendHttpMessageService");
	}
}
